home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / UUCICO / ULIB.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  22KB  |  608 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    u l i b . c                                                     */
  3. /*                                                                    */
  4. /*    Serial port interface to COMMFIFO.ASM for MS-DOS                */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: ulib.c 1.18 1993/10/03 22:09:09 ahd Exp $
  21.  *
  22.  *    $Log: ulib.c $
  23.  * Revision 1.18  1993/10/03  22:09:09  ahd
  24.  * Use unsigned long to display speed
  25.  *
  26.  * Revision 1.17  1993/09/29  13:18:06  ahd
  27.  * Use new dummy setprty function
  28.  *
  29.  * Revision 1.16  1993/09/27  04:04:06  ahd
  30.  * Normalize references to modem speed to avoid incorrect displays
  31.  *
  32.  * Revision 1.15  1993/09/27  00:45:20  ahd
  33.  * Cosmetic clean up
  34.  *
  35.  * Revision 1.14  1993/09/25  03:07:56  ahd
  36.  * Add dummy priority function
  37.  *
  38.  * Revision 1.13  1993/07/11  14:38:32  ahd
  39.  * Correct routine names in displayed messages
  40.  *
  41.  * Revision 1.12  1993/05/30  15:25:50  ahd
  42.  * Multiple driver support
  43.  *
  44.  * Revision 1.11  1993/05/30  00:08:03  ahd
  45.  * Multiple communications drivers support
  46.  * Don't lock port if not in multi-task mode
  47.  * Break trace functions out of ulib.c into commlib.c
  48.  *
  49.  * Revision 1.10  1993/05/09  03:41:47  ahd
  50.  * Make swrite accept constant input strings
  51.  *
  52.  * Revision 1.9  1993/04/11  00:33:54  ahd
  53.  * Global edits for year, TEXT, etc.
  54.  *
  55.  * Revision 1.8  1993/04/05  04:35:40  ahd
  56.  * Set/clear abort processing flags in modem.c
  57.  *
  58.  * Revision 1.7  1993/01/23  19:08:09  ahd
  59.  * Don't attempt to detect lost carrier in sread()
  60.  *
  61.  * Revision 1.6  1992/12/30  05:27:11  plummer
  62.  * MS C compile fixes
  63.  * Add CD() to sread
  64.  *
  65.  * Revision 1.5  1992/12/12  16:12:13  ahd
  66.  * Include header file for definition for memory avail routines
  67.  *
  68.  * Revision 1.4  1992/12/07  02:43:20  ahd
  69.  * Improve error message when low memory prevents COMM port install
  70.  *
  71.  * Revision 1.3  1992/11/29  22:09:10  ahd
  72.  * Use sopen() rather than FOPEN() to avoid retries on comm port
  73.  *
  74.  * Revision 1.2  1992/11/21  06:17:42  ahd
  75.  * Delete old (pre-COMMFIFO) autobaud function
  76.  *
  77.  */
  78.  
  79. /*--------------------------------------------------------------------*/
  80. /*                        System include files                        */
  81. /*--------------------------------------------------------------------*/
  82.  
  83. #include <fcntl.h>
  84. #include <io.h>
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87. #include <string.h>
  88. #include <time.h>
  89. #include <share.h>
  90.  
  91. #ifdef __TURBOC__
  92. #include <alloc.h>
  93. #endif
  94.  
  95. /*--------------------------------------------------------------------*/
  96. /*                    UUPC/extended include files                     */
  97. /*--------------------------------------------------------------------*/
  98.  
  99. #include "lib.h"
  100. #include "hlib.h"
  101. #include "ulib.h"
  102. #include "comm.h"
  103. #include "ssleep.h"
  104. #include "catcher.h"
  105. #include "commlib.h"
  106.  
  107. /*--------------------------------------------------------------------*/
  108. /*                        Internal prototypes                         */
  109. /*--------------------------------------------------------------------*/
  110.  
  111. static void ShowModem( void );
  112.  
  113. /*--------------------------------------------------------------------*/
  114. /*                          Global variables                          */
  115. /*--------------------------------------------------------------------*/
  116.  
  117. static unsigned short current_bps;
  118. static char current_direct;
  119. static boolean carrierdetect;
  120.  
  121. currentfile();
  122.  
  123. /* IBM-PC I/O routines */
  124.  
  125. /* "DCP" a uucp clone. Copyright Richard H. Lamb 1985,1986,1987 */
  126.  
  127. /*************** BASIC I/O ***************************/
  128. /* Saltzers serial package (aka Info-IBMPC COM_PKG2):
  129.  * Some notes:  When packets are flying in both directions, there seems to
  130.  * be some interrupt handling problems as far as receiving.  Checksum errors
  131.  * may therefore occur often even though we recover from them.  This is
  132.  * especially true with sliding windows.  Errors are very few in the VMS
  133.  * version.  RH Lamb
  134.  */
  135.  
  136. #define  STOPBIT  1
  137.  
  138. static int com_handle;
  139. static boolean hangup_needed = TRUE;
  140.  
  141. /*--------------------------------------------------------------------*/
  142. /*    n o p e n l i n e                                               */
  143. /*                                                                    */
  144. /*    Open the serial port for I/O                                    */
  145. /*--------------------------------------------------------------------*/
  146.  
  147. int nopenline(char *name, BPS bps, const boolean direct)
  148. {
  149.    int   value;
  150.  
  151.    if (portActive)              /* Was the port already active?     */
  152.       closeline();              /* Yes --> Shutdown it before open  */
  153.  
  154.    printmsg(15, "openline: %s, %lu", name, bps);
  155.  
  156.    current_direct = (char) (direct ? 'D' : 'M') ;
  157.  
  158.    if (sscanf(name, "COM%d", &value) != 1)
  159.    {
  160.       printmsg(0,"Communications port must be format COMx, was %s",
  161.                 name);
  162.       panic();
  163.    }
  164.  
  165.    if ( bflag[F_MULTITASK] )
  166.    {
  167.       com_handle = sopen( name, O_BINARY | O_RDWR, SH_DENYRW );
  168.                                  /* Used soly for lock abilities  */
  169.       if ( com_handle == -1 )
  170.       {
  171.          printerr( name );
  172.          return 1;
  173.       }
  174.    }
  175.  
  176.    select_port(value);
  177.    save_com();
  178.  
  179.    if (!install_com())
  180.    {
  181.       printmsg(0,"Communications handler install failed; "
  182.                   "probable cause ... memory shortage.");
  183.  
  184. #ifdef __TURBOC__
  185.       printmsg(0,"FAR DOS Memory free = %ld bytes",
  186.                   farcoreleft() );
  187. #endif
  188.  
  189.       return 1;
  190.    }
  191.  
  192.    current_bps = bps;
  193.    open_com(current_bps, current_direct, 'N', STOPBIT, 'D');
  194.    dtr_on();
  195.    ssleep(2);                 /* Wait two seconds as required by V.24   */
  196.    carrierdetect = FALSE;     /* No modem connected yet                 */
  197.  
  198.    traceStart( name );
  199.  
  200.    portActive = TRUE;     /* record status for error handler */
  201.  
  202.    return 0;
  203.  
  204. } /* nopenline */
  205.  
  206. /*--------------------------------------------------------------------*/
  207. /*    n s r e a d                                                     */
  208. /*                                                                    */
  209. /*    Read from the serial port                                       */
  210. /*                                                                    */
  211. /*    Non-blocking read essential to "g" protocol.  See               */
  212. /*    "dcpgpkt.c" for description.  This all changes in a             */
  213. /*    multi-tasking system.  Requests for I/O should get queued       */
  214. /*    and an event flag given.  Then the requesting process (e.g.     */
  215. /*    gmachine()) waits for the event flag to fire processing         */
  216. /*    either a read or a write.  Could be implemented on VAX/VMS      */
  217. /*    or DG but not MS-DOS.                                           */
  218. /*--------------------------------------------------------------------*/
  219.  
  220. unsigned int nsread(char *input,
  221.                     unsigned int wanted,
  222.                     unsigned int timeout)
  223. {
  224.    time_t start;
  225.  
  226.    hangup_needed = TRUE;
  227.  
  228.    start = time(nil(time_t)); /* Remember when we started processing */
  229.  
  230. /*--------------------------------------------------------------------*/
  231. /*                  Report the current modem status                   */
  232. /*---------------------------------